//
//  SELODPlanet.cpp
//  Space Explorer
//
//  Created by Yann Blaudin de Thé on 28/04/13.
//  Copyright (c) 2013 TeaTime. All rights reserved.
//

#include "SELODPlanet.h"


CSampleSceneNode::CSampleSceneNode(irr::scene::ISceneNode* parent, irr::scene::ISceneManager* mgr, irr::s32 id, const irr::core::stringw& disp_name, const std::string& spice_name, irr::video::ITexture* ground_tex, irr::video::ITexture* cloud_tex, irr::video::ITexture* normal_tex)
: irr::scene::ISceneNode(parent, mgr, id)
{
    Material.Wireframe = false;
    Material.Lighting = false;
    
    Vertices[0] = irr::video::S3DVertex(0,0,10, 1,1,0,
                                        irr::video::SColor(255,0,255,255), 0, 1);
    Vertices[1] = irr::video::S3DVertex(10,0,-10, 1,0,0,
                                        irr::video::SColor(255,255,0,255), 1, 1);
    Vertices[2] = irr::video::S3DVertex(0,20,0, 0,1,1,
                                        irr::video::SColor(255,255,255,0), 1, 0);
    Vertices[3] = irr::video::S3DVertex(-10,0,-10, 0,0,1,
                                        irr::video::SColor(255,0,255,0), 0, 0);
    
    /*
     The Irrlicht Engine needs to know the bounding box of a scene node.
     It will use it for automatic culling and other things. Hence, we
     need to create a bounding box from the 4 vertices we use.
     If you do not want the engine to use the box for automatic culling,
     and/or don't want to create the box, you could also call
     irr::scene::ISceneNode::setAutomaticCulling() with irr::scene::EAC_OFF.
     */
    Box.reset(Vertices[0].Pos);
    for (irr::s32 i=1; i<4; ++i)
        Box.addInternalPoint(Vertices[i].Pos);
}

/*
 Before it is drawn, the irr::scene::ISceneNode::OnRegisterSceneNode()
 method of every scene node in the scene is called by the scene manager.
 If the scene node wishes to draw itself, it may register itself in the
 scene manager to be drawn. This is necessary to tell the scene manager
 when it should call irr::scene::ISceneNode::render(). For
 example, normal scene nodes render their content one after another,
 while stencil buffer shadows would like to be drawn after all other
 scene nodes. And camera or light scene nodes need to be rendered before
 all other scene nodes (if at all). So here we simply register the
 scene node to render normally. If we would like to let it be rendered
 like cameras or light, we would have to call
 SceneManager->registerNodeForRendering(this, SNRT_LIGHT_AND_CAMERA);
 After this, we call the actual
 irr::scene::ISceneNode::OnRegisterSceneNode() method of the base class,
 which simply lets also all the child scene nodes of this node register
 themselves.
 */
void CSampleSceneNode::OnRegisterSceneNode()
{
    if (IsVisible)
        SceneManager->registerNodeForRendering(this);
    
    ISceneNode::OnRegisterSceneNode();
}

/*
 In the render() method most of the interesting stuff happens: The
 Scene node renders itself. We override this method and draw the
 tetraeder.
 */
void CSampleSceneNode::render()
{
    irr::u16 indices[] = {	0,2,3, 2,1,3, 1,0,3, 2,0,1	};
    irr::video::IVideoDriver* driver = SceneManager->getVideoDriver();
    
    driver->setMaterial(Material);
    driver->setTransform(irr::video::ETS_WORLD, AbsoluteTransformation);
    driver->drawVertexPrimitiveList(&Vertices[0], 4, &indices[0], 4, irr::video::EVT_STANDARD, irr::scene::EPT_TRIANGLES, irr::video::EIT_16BIT);
}

irr::u32 CSampleSceneNode::getMaterialCount() const
{
    return 1;
}

irr::video::SMaterial& CSampleSceneNode::getMaterial(irr::u32 i)
{
    return Material;
}

